<?php

class Admin
{
    public static $info;

    public static function Log($handle, $update)
    {
        Db2::i(Db::get(), 'admin_handle_log', [
            'admin_id' => self::$info['id'],
            'handle' => $handle,
            'update' => json_encode($update, JSON_UNESCAPED_UNICODE),
            'create_time' => Tool::date(),
            'create_ip' => Tool::ip()
        ]);
    }

    public static function CheckError($error_type = 'jump', $data = [])
    {
        switch ($error_type) {
            case 'jump':
                Page::jump(Tool::fp(Tool::ap('/login')));
                break;
            case 'post':
                Tool::exit($data);
                break;
        }
    }

    public static function AuthCheck($auth_id, $error_type = 'jump'): bool
    {
        if (Admin::$info['is_super'] == '1') return true;
        $db = Db::get();
        $auth_info = $db->getRow('select * from admin_auth where id = ?', [$auth_id]);
        if ($auth_info) {
            if ($auth_info['status'] == '1' && $auth_info['power_type'] != '3') {
                if ($auth_info['power_type'] == '1') return true;
                $admin_auth = explode(',', self::$info['auth']);
                if (in_array($auth_id, $admin_auth)) return true;
            }
        }
        switch ($error_type) {
            case 'jump':
                Page::jump(Tool::fp(Tool::ap('/System/PermissionDenied')));
                break;
            case 'post':
                Tool::exit(Check::ini(10008));
                break;
        }
    }

    public static function Check($error_type = 'jump')
    {
        $de_token = Tool::gc('AdminToken');
        $check = Check::Input(['TOKEN' => $de_token]);
        if (!$check['check']) self::CheckError($error_type, $check);
        $check = Check::AdminToken($de_token);
        if (!$check['check']) self::CheckError($error_type, $check);
        $token = $check['data'];
        $db = Db::get();
        $sql = 'select * from admin where token = ?';
        $admin_info = $db->getRow($sql, [$token['token']]);
        $check = Check::AdminInfo($admin_info);
        if (!$check['check']) self::CheckError($error_type, $check);
        self::$info = $admin_info;
    }


}